/**
 * https://leetcode.cn/problems/kth-smallest-element-in-a-bst/
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int count;
    public int ret;

    public int kthSmallest(TreeNode root, int k) {
        count = k;
        dfs(root);
        return ret;
    }
    public void dfs(TreeNode root) {
        //“count == 0” --> 剪枝
        if(root == null || count == 0) return;

        dfs(root.left);
        count--;
        if(count == 0) {
            ret = root.val;
            //剪枝
            return;
        }
        dfs(root.right);
    }
}